package com.masterclient.app.businessmodule.homemodule.view.adapter.template;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.homecms.HomeNewCmsIndexBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ConcentrationProductAdapter extends RecyclerView.Adapter {

    private Context context;
    private int convert_flag;

    private List<HomeNewCmsIndexBean> list;
    private String source;


    public ConcentrationProductAdapter(Context context, String source, List<HomeNewCmsIndexBean> list) {
        this.context = context;
        this.source = source;
        this.list = list;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_concentration_product_item,parent,false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        HomeNewCmsIndexBean shCollect = list.get(position);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        viewHolder.mRecyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        if(shCollect.getList() != null){
            ConcentrationProductItemAdapter mAdapter = new ConcentrationProductItemAdapter(context,
                    shCollect.getList(),shCollect, shCollect.getName(),source,shCollect.getId());
            viewHolder.mRecyclerView.setAdapter(mAdapter);
        }

        if (shCollect.getList() != null && shCollect.getList().size() > 0) {
            renderImage(shCollect,viewHolder);
            viewHolder.mRecyclerView.setVisibility(View.VISIBLE);
        } else {
            renderImage(shCollect,viewHolder);
            viewHolder.mRecyclerView.setVisibility(View.GONE);
        }

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    CommonUtil.goAnyWhere(context,shCollect.getUrl(),"", "", "", "","");
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private void renderImage(HomeNewCmsIndexBean shCollect,ViewHolder viewHolder) {
        int width = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(35);
        int height = width;
        GlideUtil.displayImage(context,shCollect.getBrand_image(),viewHolder.image,R.mipmap.defaults_1,R.mipmap.default_error,width,height);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.mRecyclerView)
        public RecyclerView mRecyclerView;
        @BindView(R.id.image)
        public ImageView image;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }


}

